
* GDP Per Capita as Predictor of Troop Deployments

set scheme plotplainblind

* GDP Data (2007-2015)
	clear
	import excel "INE-PeruGDPbyDepartment.xlsx", sheet("cuadro1") cellrange(A8:J32) firstrow
	
	rename A region
	
	local i = 2007
	
	foreach var of varlist B-J {
	rename `var' y_`i'
	local i = `i'+1
	}
	
		reshape long y_, i(region) j(year)
	
			rename y_ gdp

			replace region = subinstr(region, "í", "i",.)
			replace region = subinstr(region, "á", "a",.)

			replace region = upper(region)
			
	sort region
	tempfile gdp
	save `gdp', replace
	

* Deployment Data
	clear
	cd ""
	import excel "Deployment_Master-AliCopy-June2017.xlsx", sheet("Sheet1") firstrow case(lower)

	keep if country == "Peru"
	keep year country region province
	replace region = "Cusco" if region == "Cuzco"
	replace region = "Lima Province" if province == "Lima Province"
	gen deployment = 1
	
	replace region = upper(region)

	keep year region deployment
	
		collapse (count) deployment, by(region year)

	tempfile clear
	tempfile deploymentcount
	save `deploymentcount', replace
	
	
* Population Data (2007-2008)
	clear
	import excel "INE-PeruPopulationbyDepartment.xls", sheet("2006-2008") cellrange(A8:K32)
	
	keep B F I
	rename B region
	rename F y_2007
	rename I y_2008
	
		reshape long y_, i(region) j(year)
		
			rename y_ pop
			
			sort region
			
	tempfile pop20072008
	save `pop20072008', replace
	
* Population Data (2009-2011)
	clear
	import excel "INE-PeruPopulationbyDepartment.xls", sheet("2009-2011") cellrange(A8:K32)
	
	keep B C F I
	rename B region
	rename C y_2009
	rename F y_2010
	rename I y_2011
	
		reshape long y_, i(region) j(year)
		
			rename y_ pop
			
			sort region
			
	tempfile pop20092011
	save `pop20092011', replace
		
		
* Population Data (2012-2015)
	clear
	import excel "INE-PeruPopulationbyDepartment.xls", sheet("2012-2015") cellrange(A8:N32)
	
	keep B C F I L
	rename B region
	rename C y_2012
	rename F y_2013
	rename I y_2014
	rename L y_2015
	
		reshape long y_, i(region) j(year)
		
			rename y_ pop
			
			sort region 
			
	tempfile pop20122015
	save `pop20122015', replace
	
		append using `pop20092011' `pop20072008'
	
		sort region year
		
		replace region = subinstr(region, "Í", "I",.)
		replace region = subinstr(region, "Á", "A",.)
		
		sort region year
		
		merge 1:1 region year using `gdp', gen(merge_gdp)
		
		merge 1:1 region year using `deploymentcount', gen(merge_deployment)
		
		recode deployment (. = 0)
	
		replace region = "LIMA" if region == "CALLAO"
		replace region = "LIMA" if region == "LIMA PROVINCE"
		
		collapse (sum) pop gdp deployment, by(region year)
	
		encode region, gen(regioncode)
		xtset regioncode year
		gen gdppc = gdp/pop
		gen ln_gdppc = ln(gdppc)
		gen ln_pop = ln(pop)
		gen ln_gdp = ln(gdp)
		
		gen deploydummy = 0
		replace deploydummy = 1 if deployment>0
		
		gen growth = ((gdppc-l.gdppc)/l.gdppc)*100
		
	collapse (mean) gdppc (sum) deployment, by(region)
	
	replace region = "LIMA*" if region == "LIMA"
	
	graph hbar (sum) deployment, over(region, sort(gdppc)) bar(1, bfcolor(gs8)) ///
	ytitle(Count) scale(.9) ///
	name(bargraph, replace) 
	
	do "Figures_Map_Peru_20171009.do"
	
	*graph combine mapgraph bargraph, cols(2) imargin(0 0 0 0)
	
	*graph export "/Users/michaelflynn/Dropbox/Projects/Troops and Public Opinion in Latin America/Figures/Figure_Map_Bar_Graph.pdf", replace
	
	graph combine mapgraphcolor bargraph, cols(2) imargin(0 0 0 0)
	
	graph export "Figure_Map_Bar_Graph_Color.pdf", replace
